package com.lhj.library.application.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.lhj.library.application.convert.UserDtoConverter;
import com.lhj.library.application.dto.UserDTO;
import com.lhj.library.common.entity.PageResult;
import com.lhj.library.common.entity.Result;
import com.lhj.library.domain.entity.UserBO;
import com.lhj.library.domain.service.UserDomainService;
import com.lhj.library.infra.basic.entity.Book;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserDomainService userDomainService;


    /**
     * 修改用户
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody UserDTO userDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(userDTO));
            }
            Preconditions.checkNotNull(userDTO.getId(), "用户id不能为空");
            UserBO userBO = UserDtoConverter.INSTANCE.convertDtoToUserBo(userDTO);
            return Result.ok(userDomainService.update(userBO));
        } catch (Exception e) {
            return Result.fail("修改失败");
        }
    }

    /**
     * 删除用户
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody UserDTO userDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.delete.dto:{}", JSON.toJSONString(userDTO));
            }
            Preconditions.checkNotNull(userDTO.getId(), "用户id不能为空");
            UserBO userBO = UserDtoConverter.INSTANCE.convertDtoToUserBo(userDTO);
            return Result.ok(userDomainService.delete(userBO));
        } catch (Exception e) {
            return Result.fail("删除失败");
        }
    }

    /**
     * 用户状态（禁用/启用）
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/changeStatus")
    public Result<Boolean> changeStatus(@RequestBody UserDTO userDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.changeStatus.dto:{}", JSON.toJSONString(userDTO));
            }
            Preconditions.checkNotNull(userDTO.getId(), "用户id不能为空");
            UserBO userBO = UserDtoConverter.INSTANCE.convertDtoToUserBo(userDTO);
            return Result.ok(userDomainService.changeStatus(userBO));
        } catch (Exception e) {
            return Result.fail("修改状态失败");
        }
    }

    /**
     * 查询用户
     *
     * @param userDTO
     * @return
     */
    @PostMapping("/select")
    public Result<UserDTO> select(@RequestBody UserDTO userDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.select.dto:{}", JSON.toJSONString(userDTO));
            }
            UserBO userBO = UserDtoConverter.INSTANCE.convertDtoToUserBo(userDTO);
            UserBO queryUserBo = userDomainService.select(userBO);
            UserDTO queryUserDto = UserDtoConverter.INSTANCE.convertBotoDto(queryUserBo);
            return Result.ok(queryUserDto);
        } catch (Exception e) {
            return Result.fail("查询失败");
        }
    }

    /**
     * 查询用户列表
     *
     * @return
     */
    @PostMapping("/selectUserPage")
    public Result<PageResult<Book>> selectUserPage(@RequestBody UserDTO userDTO) {
        try {

            if (log.isInfoEnabled()) {
                log.info("UserController.selectUserPage.dto:{}", JSON.toJSONString(userDTO));
            }
            UserBO userBO = UserDtoConverter.INSTANCE.convertDtoToUserBo(userDTO);
            PageResult<UserBO> boUserResult = userDomainService.selectUserPage(userBO);
            PageResult<UserDTO> userDTOPageResult = UserDtoConverter.INSTANCE.convertBoToDtoPage(boUserResult);
            return Result.ok(userDTOPageResult);
        } catch (Exception e) {
            return Result.fail("分页查询用户失败");
        }
    }

}
